//入口文件
import Vue from "vue"
import VueRouter from "vue-router"
Vue.use(VueRouter);
import Vuex from "vuex"
Vue.use(Vuex);
//每次进入网站时，首先会调用main.js，先从本地存储中，把购物车的数据读出来，放到store中
var car = JSON.parse(localStorage.getItem("car") || "[]");
var store = new Vuex.Store({
    state: {
        car: car,//保存购物车中商品的数据

    },
    mutations: {
        addToCar(state,goodsinfo){
        //    点击“加入购物车”按钮，把商品信息保存到store中
        //    假设 在购物车中，没有找到对应的商品
            var flag = false;
            state.car.some(item => {
                if(item.id === goodsinfo.id){
                    item.count += parseInt(goodsinfo.count);
                    flag = true;
                    return true;
                }
            });
            //若循环完毕，flag还是false，则把商品直接push到数组中
            if(!flag){
                state.car.push(goodsinfo);
            }
        //    当更新car之后，将car数组保存到本地
            localStorage.setItem("car",JSON.stringify(state.car));
        },
        updateGoodsInfo(state,goodsinfo){
            state.car.some(item => {
                if(item.id === goodsinfo.id){
                    item.count = parseInt(goodsinfo.count);
                    return true;
                }
            });
            //当修改完商品数量之后，把最新的数量存到本地
            localStorage.setItem("car",JSON.stringify(state.car));
        },
        removeForCar(state,id){
        //    根据id，删除store中购物车对应的商品数据
            state.car.some( (item,index) => {
                if(item.id === id){
                    state.car.splice(index, 1);
                    return true;
                }
            });
            //将删除完毕后的最新购物车数据同步到本地存储中
            localStorage.setItem("car",JSON.stringify(state.car));
        },
        updateGoodsSelected(state,info){
            state.car.forEach(item => {
                if(item.id === info.id){
                    item.selected = info.selected;
                }
            });
            //将更新的商品状态同步到本地
            localStorage.setItem("car",JSON.stringify(state.car));
        }
    },
    getters: {
        getAllCount(state){
            var c = 0;
            state.car.forEach(item => {
                c += item.count;
            });
            return c;
        },
        getGoodsCount(state){
            var o = {};
            state.car.forEach(item => {
                o[item.id] = item.count;
            });
            return o;
        },
        getGoodsSelected(state){
            var o = {};
            state.car.forEach( item => {
                o[item.id] = item.selected;
            });
            return o;
        },
        getGoodsCountAndAmount(state){
            var o = {
                count: 0,//勾选的数量
                amount: 0//勾选的数量
            };
            state.car.forEach(item => {
                if(item.selected){
                    o.count += item.count;
                    o.amount += item.price * item.count;
                }
            });
            return o;
        }
    }
});
//导入router.js模块
import router from "./router"
//导入mint-ui的组件
//import { Header,Swipe,SwipeItem,Button,Lazyload} from "mint-ui"
import MintUI from "mint-ui"
Vue.use(MintUI);
import 'mint-ui/lib/style.css'
//导入MUI
import "./lib/mui/css/mui.css"
import "./lib/mui/css/icons-extra.css"
//导入App组件
import app from "./App.vue"
//导入vue-resource
import VueResource from "vue-resource"
//导入格式化时间的插件
import moment from "moment"
//    导入图片预览插件
import VuePreview from 'vue-preview'
Vue.use(VuePreview);
//定义全局过滤器
//时间过滤器
Vue.filter("dateFormat",function(dataStr,pattern = "YYYY-MM-DD HH:mm:ss"){
    return moment(dataStr).format(pattern)
});
Vue.use(VueResource);
//设置请求根路径
Vue.http.options.root = "/src/static/";
//全局设置post时表单数据格式的组织形式
Vue.http.options.emulateJSON = true;
//安装路由
//Vue.component(Header.name,Header);
//Vue.component(Swipe.name, Swipe);
//Vue.component(SwipeItem.name, SwipeItem);
//Vue.component(Button.name, Button);
//Vue.use(Lazyload);
var vm = new Vue({
    el: "#app",
    render: c => c(app),
    router,
    store
});